//
//  AY_BookSubDetailPage.m
//  AiYue
//
//  Created by boWen on 2023/10/25.
//

#import "AY_BookSubDetailPage.h"
#import "AY_BookSubDetailCell.h"
#import "AY_BookDetailViewController.h"

@interface AY_BookSubDetailPage ()<UITableViewDelegate, UITableViewDataSource, SXLoadingDelegate>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) NSMutableArray * dataAry;
@property (strong,nonatomic) AY_LoadingView *loadingView;
@property (nonatomic, strong) AY_RefreshHeader *header;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

@implementation AY_BookSubDetailPage

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self barStyleBlack];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configDataUI];
}

- (void)configDataUI {
    self.gk_navTitle = kStringUtils(self.categoaryName);
    [self.view addSubview:self.baseTableView];
    [self getConfigNet];
}


- (void)getConfigNet {
    [self.loadingView loadingInView:self.view];
    [AYApiRequest postArticleListByCategoryCategoryId:self.categoaryId wordType:@"0" articleStatus:@"0" pageNum:@"1" pageSize:@"10" Success:^(AY_CategoaryModel * _Nonnull pkg) {
        [self.loadingView dismiss];
        [self.baseTableView.mj_header endRefreshing];
        if([pkg.code isEqualToString:@"200"]) {
            self.dataAry = [NSMutableArray arrayWithArray:pkg.rows];
        }
        [self.baseTableView reloadData];
    } failure:^(int status, NSString * _Nonnull info) {
        [self.baseTableView.mj_header endRefreshing];
        [self.baseTableView.mj_footer endRefreshing];
        [self.loadingView refresh];
    }];
}

- (void)loadNewData {
    self.page = 1;
    [self loadData];
}

- (void)loadMoreData {
    self.page ++;
    [self loadData];
}

- (void)loadData {
    [AYApiRequest postArticleListByCategoryCategoryId:self.categoaryId wordType:@"0" articleStatus:@"0" pageNum:[NSString stringWithFormat:@"%ld",self.page] pageSize:@"10" Success:^(AY_CategoaryModel * _Nonnull pkg) {
        [self.baseTableView.mj_header endRefreshing];
        if([pkg.code isEqualToString:@"200"]) {
            if (self.page == 1) {
                [self.dataAry removeAllObjects];
                self.dataAry = [NSMutableArray arrayWithArray:pkg.rows];
            }else {
                [self.dataAry addObjectsFromArray:pkg.rows];
            }
            if (self.dataAry.count >= pkg.total.integerValue) {
                [self.baseTableView.mj_footer endRefreshingWithNoMoreData];
            }else {
                [self.baseTableView.mj_footer endRefreshing];
            }
        }
        [self.baseTableView reloadData];
    } failure:^(int status, NSString * _Nonnull info) {
        [self.baseTableView.mj_header endRefreshing];
        [self.baseTableView.mj_footer endRefreshing];
        [self.loadingView refresh];
    }];
}

- (MJRefreshAutoNormalFooter *)footer
{
    if (!_footer) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _footer.stateLabel.hidden = NO;
        _footer.stateLabel.font = [UIFont systemFontOfSize:12.0];
        _footer.stateLabel.textColor = RGB(150,150,150,1);
        [_footer setTitle:@"正在加载中" forState:MJRefreshStateRefreshing];
        [_footer setTitle:@"我也是有底线的" forState:MJRefreshStateNoMoreData];
    }
    return _footer;
}

- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, SCREEN_H - GK_STATUSBAR_NAVBAR_HEIGHT - YNUM(20))];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor whiteColor];
        myTableView.mj_header = self.header;
        myTableView.mj_footer = self.footer;
        [myTableView registerClass:[AY_BookSubDetailCell class] forCellReuseIdentifier:@"AY_BookSubDetailCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (NSMutableArray *)dataAry {
    if(!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

- (AY_LoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[AY_LoadingView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        _loadingView.backgroundColor = COLOR(@"#F7F5F6");
        _loadingView.delegate = self;
        _loadingView.hideLeftpop = NO;
    }
    return _loadingView;
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookSubDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookSubDetailCell"];
    if(self.dataAry.count > indexPath.row) {
        cell.bookModel = self.dataAry[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(109);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    if(self.dataAry.count > indexPath.row) {
        bookDetailVC.bookId = [self.dataAry[indexPath.row] bookId];
    }
    [self.navigationController pushViewController:bookDetailVC animated:YES];
}

- (AY_RefreshHeader *)header
{
    if (!_header) {
        _header = [AY_RefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        _header.lastUpdatedTimeLabel.hidden = YES;
        _header.stateLabel.hidden = NO;
        [_header setTitle:@"松手更新" forState:MJRefreshStateIdle];
        [_header setTitle:@"松手更新" forState:MJRefreshStateRefreshing];
        [_header setTitle:@"松手更新" forState:MJRefreshStatePulling];
    }
    return _header;
}

- (void)refreshAction {
    [self getConfigNet];
}

- (void)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}



@end
